﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QFD.Controller
{
    public class ControllerCoorporacao
    {
        public static void addCoorporacao(COORPORACAO obj)
        {
            using (QFDDataContext context = new QFDDataContext())
            {
                context.COORPORACAOs.InsertOnSubmit(obj);
                context.SubmitChanges();
            }
        }

        public static void updateCoorporacao(COORPORACAO obj)
        {
            using (QFDDataContext context = new QFDDataContext())
            {
                COORPORACAO coorp = context.COORPORACAOs.Where(coorporacao => coorporacao.CDCOORPORACAO == obj.CDCOORPORACAO).FirstOrDefault();
                coorp.CDCOORPORACAO = obj.CDCOORPORACAO;
                coorp.NMCOORPORACAO = obj.NMCOORPORACAO;
                context.SubmitChanges();
            }
        }

        public static COORPORACAO getByCoorporacao(int cd)
        {
            COORPORACAO objCoorporacao = null;
            using (QFDDataContext context = new QFDDataContext())
            {
                objCoorporacao =
                    (from coorporacao in context.COORPORACAOs
                     where coorporacao.CDCOORPORACAO == cd
                     select coorporacao).FirstOrDefault();
            }
            return objCoorporacao;
        }

        public static List<COORPORACAO> getListaCoorporacao()
        {
            //seta a variável lista            
            List<COORPORACAO> lista = null;

            using (QFDDataContext context = new QFDDataContext())
            {
                IQueryable<COORPORACAO> query =
                from coorporacao in context.COORPORACAOs
                orderby coorporacao.CDCOORPORACAO ascending
                select coorporacao;
                lista = query.ToList();
            }
            return lista;
        }

        public static void deleteCoorporacao(COORPORACAO obj)
        {
            using (QFDDataContext context = new QFDDataContext())
            {
                COORPORACAO coorp = context.COORPORACAOs.Where(coorporacao => coorporacao.CDCOORPORACAO == obj.CDCOORPORACAO).FirstOrDefault();
                if (coorp != null)
                {
                    context.COORPORACAOs.DeleteOnSubmit(coorp);
                    context.SubmitChanges();
                }
            }
        }
    }
}
